package com.block.risk.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.block.common.exception.NotifyExceptionService;
import com.block.common.model.dto.CountInfoDTO;
import com.block.common.service.ICountCommonService;
import com.block.common.service.LogFactory;
import com.block.db.entity.MemberExtInfo;
import com.block.db.entity.RiskBaseInfo;
import com.block.risk.factory.RiskTypeFactory;
import com.block.risk.model.param.RiskControlParam;
import com.block.risk.model.result.RiskControlResult;
import com.block.risk.service.IRiskBaseInfoService;
import com.block.risk.service.IRiskHandlerService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RiskHandlerServiceImpl implements IRiskHandlerService {

    private static Logger log = LogFactory.getRiskLog();

    @Autowired
    IRiskBaseInfoService riskBaseInfoService;
    @Autowired
    NotifyExceptionService notifyExceptionService;
    @Autowired
    ICountCommonService countCommonService;

    /**
     * 数据风险控制
     * @param memberExtInfo
     * @param dataId
     * @param dataType
     * @return
     */
    @Override
    public boolean dataRiskControlHandle(MemberExtInfo memberExtInfo, String dataId, String dataType){

        try {
            if (StrUtil.hasEmpty(memberExtInfo.getUsername(), memberExtInfo.getMobile(), memberExtInfo.getIdCard())){
                log.info("【风控过滤】用户三要素信息不全【uid】{}", memberExtInfo.getUid());
                return false;
            }

            // 查询商户风控配置信息
            RiskBaseInfo riskBaseInfo =  riskBaseInfoService.getEntityByDataIdAndType(dataId, dataType);
            if (ObjectUtil.isEmpty(riskBaseInfo)){
                // 风控配置为空，或者不可用的时候，返回风控通过
                log.info("【风控过滤】无风控数据或风控不可用【dataId】{}【dataType】{}", dataId, dataType);
                return false;
            }

            RiskControlParam riskParamInfo = new RiskControlParam();
            riskParamInfo.setMemberExtInfo(memberExtInfo);
            riskParamInfo.setDataId(dataId);
            riskParamInfo.setDataType(dataType);
            riskParamInfo.setRiskBaseInfo(riskBaseInfo);

            // 执行风控具体的操作
            RiskControlResult riskResultInfo = RiskTypeFactory.getService(riskBaseInfo.getFactoryType()).riskHandlerExec(riskParamInfo);

            log.info("【风控】风控结果【uid】{}【riskResultInfo】{}【dataId】{}【dataType】{}",  memberExtInfo.getUid(), JSON.toJSONString(riskResultInfo),dataId, dataType);

            if (riskResultInfo.isSuccess()){
                // 风控通过
                CountInfoDTO countInfoDTO = new CountInfoDTO(memberExtInfo.getChannelCode());
                countInfoDTO.setRiskSuccessNumber(1);
                countCommonService.calculateDataInfo2(countInfoDTO);
            }

            return riskResultInfo.isSuccess();
        }catch (Exception exception){
            exception.printStackTrace();
            String message = StrUtil.format("风控异常【uid】{}【dataId】{}【dataType】{}", memberExtInfo.getUid(), dataId, dataType);
            notifyExceptionService.notifyNoticeInfo("riskControl", "机构风控", message, exception);
            return false;
        }
    }

}
